package top.yxqz.ck;



import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author Mr.余
 * @date 2022/11/23
 * @package top.yxqz.ck
 * 存cookie，请记住中文有问题
 * HttpServletResponse 对象
 */

@WebServlet("/demo1")
public class CookieDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //        Cookie，只能存字符串 kv键值对的方式
//        注意，默认存储中文，会报错 字符问题
//        如果name已经存在，则value覆盖，否则就是创建一个新的kv值

//        Cookie cookie = new Cookie("uname", URLEncoder.encode(uname, "UTF-8") );
//        Cookie cookie = new Cookie("student", URLEncoder.encode("张三", "UTF-8"));
        Cookie cookie = new Cookie(URLEncoder.encode("姓名：", "UTF-8"), URLEncoder.encode("张三", "UTF-8"));
//        设置Cookie
        response.addCookie(cookie);
    }
}
